package server.handler.groupHandler;

import com.alibaba.fastjson.JSONObject;
import common.EnvelopeMessage;
import common.bodies.GroupShipBody;
import server.dao.GroupDao;
import server.dao.UserDao;
import server.handler.SimpleHandler;
import server.session.Session;

import java.util.List;

public class GroupKickHandler implements SimpleHandler {
    @Override
    public void handle(EnvelopeMessage msg, Session session) {
        JSONObject body = msg.getBody();
        Integer groupId = body.getInteger("groupId");
        List<String> kickedMembers = body.getJSONArray("kickedMembers").toJavaList(String.class);
        Integer userId = body.getInteger("userId");
        String groupName = body.getString("groupName");
        for (String memberName : kickedMembers) {

            Integer memberId = new UserDao().getUserIdByUsername(memberName);
            GroupShipBody ship = new GroupShipBody(memberId, groupId, groupName, 0, -1, null);
            new GroupDao().updateGroupShip(ship);
            JSONObject response = new JSONObject();
            response.put("status", true);
            EnvelopeMessage back = new EnvelopeMessage("group", "kickSuccess", response);
            String json = JSONObject.toJSONString(back);
            System.out.println("服务端返回" + json);
            session.send(json);
        }

    }
}